package com.zjitc.controller;

import com.zjitc.dao.CartDao;
import com.zjitc.model.CartItem;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.CartService;
import com.zjitc.service.UserControllerService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/19 15:48
 * @description:
 */
@Controller
public class UserCart {
  @Autowired
  private CartService cartService;
  private ModelAndView mv=new ModelAndView();
  @RequestMapping("/addCart")
  public ModelAndView addCart(@RequestParam("pid") Integer pid,
                              HttpSession session){
    User user = (User) session.getAttribute("user");
    if (user==null){
      mv.setView(new InternalResourceView("/WEB-INF/user/login.jsp"));
      return mv;
    }
    Product product=cartService.findcartPid(pid);
    CartItem cart=new CartItem();
    cart.setPid(product.getPid());
    cart.setUid(user.getUid());
    cart.setCartid(cart.getPid());
    cart.setTotal(product.getShop_price());
    cart.setQuantity(1);
    cart.setProduct(product);
    cartService.addCart(cart);
    List<CartItem> list = cartService.listCart();
    mv.addObject("cart",cart);
    mv.addObject("list",list);
    mv.setView(new InternalResourceView("/WEB-INF/user/cart.jsp"));
    return mv;
  }

  @RequestMapping("/removeCar")
  public ModelAndView removeCar(@RequestParam("pid")Integer pid){
    cartService.remover(pid);
    mv.setView(new InternalResourceView("/WEB-INF/user/cart.jsp"));
    return mv;
  }
  @RequestMapping("/clearCart")
  public ModelAndView clearCart(){
    cartService.delete();
    mv.addObject("mv",mv);
    mv.setView(new InternalResourceView("WEB-INF/user/cart.jsp"));
    return mv;
  }
  @RequestMapping("/myCart")
    public ModelAndView myCart(){
      mv.setView(new InternalResourceView("WEB-INF/user/cart.jsp"));
      return mv;
  }
}
